package com.control.mbtc.fragments.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.control.mbtc.R;
import com.zzyd.common.weight.lines.KlineOne;
import com.zzyd.factory.data.market.MarketBean;
import com.zzyd.factory.model.app.Account;

import java.util.List;

/**
 * @author wjunjie
 * @version 1.0
 * @E-mail wang_junjie007@163.com
 * @company ZZYD
 * @createTime 2018/8/28 11:07
 * @类注释:
 */
public class ExpanListCommonAdapter extends BaseExpandableListAdapter {
    public static final int TYPE_LJJY = 0x01;
    private Context context;
    private List<MarketBean.TradesBean> list;
    private CallbackView callbaclView;

    public ExpanListCommonAdapter(Context context, List<MarketBean.TradesBean> list, CallbackView callback) {
        this.context = context;
        this.list = list;
        this.callbaclView = callback;
    }

    public ExpanListCommonAdapter(Context context, List<MarketBean.TradesBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getKlines().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TitleViewHodel viewHodel;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.expand_listview_itme_layout, null);
            viewHodel = new TitleViewHodel();
            viewHodel.setViews(convertView);
            convertView.setTag(viewHodel);
        } else {
            viewHodel = (TitleViewHodel) convertView.getTag();
        }
        viewHodel.tv_name.setText(list.get(groupPosition).getCoinName());
        viewHodel.tv_price.setText(list.get(groupPosition).getPriceByFB());
        if (Account.getModelIsCN()) {
            viewHodel.tv_prives.setText("￥ " + list.get(groupPosition).getPriceByCNY());
        } else {
            viewHodel.tv_prives.setText("$ " + list.get(groupPosition).getPriceByUsd());
        }
        if (list.get(groupPosition).getUpAndDowns() >= 0) {
            viewHodel.bt_percenter.setBackgroundResource(R.drawable.bg_increase_red);
            viewHodel.bt_percenter.setText("+" + list.get(groupPosition).getUpAndDowns() + " %");
        } else {
            viewHodel.bt_percenter.setBackgroundResource(R.drawable.bg_increase_green);
            viewHodel.bt_percenter.setText(list.get(groupPosition).getUpAndDowns() + " %");
        }
        Glide.with(context)
                .load(list.get(groupPosition).getPicUrl())
                .into(viewHodel.im_view);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        KlinesViewHodel viewHodel;

        convertView = LayoutInflater.from(context).inflate(R.layout.contents_lines, null);
        viewHodel = new KlinesViewHodel();
        viewHodel.setViews(convertView);

        viewHodel.tv_cjl.setText(list.get(groupPosition).getDayCountMax());
        viewHodel.tv_zdj.setText(list.get(groupPosition).getDayPriceMin());
        viewHodel.tv_zgj.setText(list.get(groupPosition).getDayPriceMax());
        List<List<String>> klines = list.get(groupPosition).getKlines();
        double[] doubles = new double[klines.size()];
        for (int i = 0; i < klines.size(); i++) {
            List<String> strings = klines.get(i);
            if (strings.size() == 2) {
                doubles[i] = Double.parseDouble(strings.get(1));
            }
        }
        long[] times = new long[klines.size()];
        for (int i = 0; i < klines.size(); i++) {
            List<String> strings = klines.get(i);
            if (strings.size() == 2) {
                times[i] = Long.parseLong(strings.get(0));
            }
        }

        viewHodel.klineOne
                .setDate(doubles)
                .setTimes(times);

        viewHodel.bt_ljjy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callbaclView != null) {
                    callbaclView.onChildClick(childPosition, TYPE_LJJY);
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public interface CallbackView {
        /**
         * group点击监听
         *
         * @param groupId
         */
        void onGroupClick(int groupId, int count);

        /**
         * item点击监听
         *
         * @param childId
         */
        void onChildClick(int childId, int type);
    }

    class TitleViewHodel {
        private TextView tv_name, tv_price, tv_prives, bt_percenter;
        private ImageView im_view;

        void setViews(View root) {
            im_view = root.findViewById(R.id.im_currency);
            tv_name = root.findViewById(R.id.txt_currency_name);
            tv_price = root.findViewById(R.id.txt_currency_price_us);
            tv_prives = root.findViewById(R.id.txt_currency_price_en);
            bt_percenter = root.findViewById(R.id.txt_currency_increase);
        }
    }

    class KlinesViewHodel {
        private TextView tv_zgj, tv_zdj, tv_cjl, bt_ljjy;
        private KlineOne klineOne;

        void setViews(View root) {
            tv_zgj = root.findViewById(R.id.txt_24hzgj);
            tv_zdj = root.findViewById(R.id.txt_24hzdj);
            tv_cjl = root.findViewById(R.id.txt_24cjl);
            bt_ljjy = root.findViewById(R.id.btn_ljjy);
            klineOne = root.findViewById(R.id.kline);
        }
    }
}
